package network;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JProgressBar;

public class FileClient extends JFrame implements Runnable{
	
	String fileName;
	Socket socket;
	long fileSize=0;

	private JPanel contentPane;
	private JTextField filenameIn;
	private JButton btnSearch;
	private JButton btnSend;
	private JLabel lblResult;
	private JTextField ipIn;
	private JProgressBar progressBar;
	private JLabel status;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileClient frame = new FileClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public FileClient() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 614, 129);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getFilenameIn());
		contentPane.add(getBtnSearch());
		contentPane.add(getBtnSend());
		contentPane.add(getLblResult());
		contentPane.add(getIpIn());
		
		
		try{
			InetAddress ia = InetAddress.getLocalHost(); // 주소를 가져와 타이틀을 IP주소로 변경해준다.			
			setTitle("IP Address: " + ia.getHostAddress() + ", Port Number : 4444");
			ipIn.setText(ia.getHostAddress());
			contentPane.add(getProgressBar());
			contentPane.add(getStatus());
			
		}catch(Exception ei){
			ei.printStackTrace();
		}
		
	}

	@Override
	public void run() {
		try{
			socket = new Socket(ipIn.getText(),4444);
			
			fileName = filenameIn.getText();
			File f = new File(fileName);
			if(!f.exists()){
				JOptionPane.showMessageDialog(null, "파일이 존재하지 않습니다.");
				socket.close();
				return;
			}
			
			FileInputStream fis = new FileInputStream(f);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			
			dos.writeUTF(f.getName());
			dos.flush();
			
			byte[] b = new byte[1024];
			
			int n=0;
			int totLen=0;
			fileSize=0;
			
			while((n=fis.read(b)) != -1){
				dos.write(b,0,n);
				fileSize+=n;
				totLen+=n;
				
				progressBar.setValue(totLen);				
			}
			dos.close();
			fis.close();
			socket.close();
			JOptionPane.showMessageDialog(null, "전송완료");
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	
	public void send(){
		Thread t = new Thread(this);
		t.start();
	}
	public void openFile(){
		JFileChooser jc = new JFileChooser();
		int yn = jc.showOpenDialog(null);
		if(yn == JFileChooser.APPROVE_OPTION){
			filenameIn.setText(jc.getSelectedFile().toString());
			fileSize = jc.getSelectedFile().length();
			progressBar.setMaximum((int)fileSize);			
		}
	}
	private JTextField getFilenameIn() {
		if (filenameIn == null) {
			filenameIn = new JTextField();
			filenameIn.setBounds(12, 10, 465, 21);
			filenameIn.setColumns(10);
		}
		return filenameIn;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openFile();
				}
			});
			btnSearch.setBounds(489, 9, 97, 23);
		}
		return btnSearch;
	}
	private JButton getBtnSend() {
		if (btnSend == null) {
			btnSend = new JButton("Send");
			btnSend.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					send();
				}
			});
			btnSend.setBounds(489, 42, 97, 23);
		}
		return btnSend;
	}
	private JLabel getLblResult() {
		if (lblResult == null) {
			lblResult = new JLabel("Server IP Address");
			lblResult.setHorizontalAlignment(SwingConstants.RIGHT);
			lblResult.setBounds(223, 46, 126, 15);
		}
		return lblResult;
	}
	private JTextField getIpIn() {
		if (ipIn == null) {
			ipIn = new JTextField();
			ipIn.setHorizontalAlignment(SwingConstants.CENTER);
			ipIn.setBounds(361, 43, 116, 21);
			ipIn.setColumns(10);
		}
		return ipIn;
	}
	private JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setBounds(12, 71, 574, 14);
		}
		return progressBar;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("New label");
			status.setBounds(12, 46, 104, 15);
		}
		return status;
	}
}
